<template>
    <!-- 商家活动页面 -->
    <div class="store-activity">
        <div class="no-data-wrap displayFlex flexCenter" v-if="pageMsg && pageMsg.list && pageMsg.list.length == 0">
            <v-no-data v-if="noDataShow"
                :noDataProps="{text:'暂无内容哦～',imgUrl:staticImageUrl('/images/look/no_comment_list1.png')}"></v-no-data>
        </div>
        <!--:swiperMsgProp="swiperMsgProp"-->
        <full-img-video-swiper v-else-if=" Array.isArray(pageMsg.list) && pageMsg.list.length > 0 && showSwiper"
            ref="fullImgVideoSwiper" :rtCommontListProps="commontList" :commentListProps="commentListProps"
            :swiperContentProp="pageMsg" :couponsListProp="couponsList" @wxShareCb="wxShareCb" @isLike="isLike"
            @showShare="shareShow" @suspensionCb="suspensionCb" @getAllCounponsFn="getAllCounponsFn"
            @animationCb="animationCb" @openRedPage="openRedPage" @getTemplate="getTemplate"
            @showMarsterDetail="showMarsterDetail" @showMarsterClassify="showMarsterClassify" @doAppraise="doAppraise">
            <div slot="swiperIn" slot-scope="swiperItem" v-if="pageType == 'buyShow'">
                <video-float-footer :bottomSuspensionProp="Object.assign({},swiperItem.swiperItem,{type:'buyShow'})">
                </video-float-footer>
            </div>
        </full-img-video-swiper>
        <div slot="swiperOut"
            v-if="pageType == 'master' && bottomSuspensionShow && master.masterMsg && Array.isArray(master.masterMsg.list) && master.masterMsg.list.length > 0">
            <bottom-suspension ref="bottomSuspension" v-for="(value,key) in master.masterMsg.list" :key="key"
                v-if="swiperIndex == key" :masterFooterProp="value.bottomSuspension.content"
                @toSwiperIndex="toSwiperIndex"></bottom-suspension>
        </div>
    </div>
</template>

<script>
import FullImgVideoSwiper from "../../components/commonActModel/panoramaVideoSwiper/FullImgVideoSwiper";
import { mapState, mapMutations, mapGetters } from 'vuex';

export default {
    name: "StoreSwiper",
    data () {
        return {
            storeId: this.$route.query.storeId,
            commentListProps: { userHendleType: 'module5' },//侧边栏评论分享等
            couponsList: [],
            commontList: [],
            swiperIndex: 0,
            noDataShow: false,
            pageType: '',
            shareType: 1,
            bottomSuspensionShow: false,//大师是否显示
            menuType: this.$route.query.menuType,
            menuId: this.$route.query.menuId,
            showSwiper: true,
            pageMsg: {
                list: []
            }
        }
    },
    computed: {
        ...mapState({
            promotion: state => state.logic.promotion,
            buyShow: state => state.logic.buyShow,
            master: state => state.logic.masterBoxsObj,
        }),
        ...mapGetters({ storeMenu: 'getterStoreMenu' }),
    },
    mounted () {
        this.updateStoreMenu();
        this.init();
    },
    methods: {
        ...mapMutations(['promotionApi', 'buyShowApi', 'masterListApi', 'updateStoreMenu', 'isLike', 'shareShow']),
        init () {
            if (this.$route.fullPath.includes('/store/master/')) {
                this.pageType = 'master';
                this.shareType = 5;
                this.initBigMaster();
            } else if (this.$route.fullPath.includes('/store/buyShow/')) {
                this.pageType = 'buyShow';
                this.shareType = 4;
                this.initBuyShow();
            } else if (this.$route.fullPath.includes('/store/goods/')) {
                this.pageType = 'goods';
                this.shareType = 3;
            } else if (this.$route.fullPath.includes('/store/panorama/')) {
                this.pageType = 'panorama';
                this.shareType = 1;
            } else if (this.$route.fullPath.includes('/store/playDirect/')) {
                this.pageType = 'playDirect';
                this.shareType = 6;
            } else if (this.$route.fullPath.includes('/store/promotion/')) {
                this.pageType = 'promotion';
                this.shareType = 2;
                this.initActivityMsg();
            }
        },
        initActivityMsg (obj) {
            let that = this;
            this.noDataShow = false;
            let queryUserId = '';
            this.shareType = 2;
            let shareId = 2; //----对应分享类型需要传的ID： 1： 媒体ID 2： 活动id 3:商品id 4:买家秀ID 5: 大师ID
            let params = {
                "pageNum": 1,
                "pageSize": 5,
                "storeId": this.storeId,
            };
            this.promotionApi(Object.assign({}, params, { successFn }));
            async function successFn () {
                if (that.promotion) {
                    that.couponsList = that.promotion.couponsList;
                    that.commontList = that.promotion.commont;
                    that.pageMsg = that.promotion.message;
                    setTimeout(() => {
                        that.noDataShow = true;
                    }, 500)
                }
            }
        },
        initBuyShow (obj) {
            /*好评/买家秀*/
            let that = this;
            this.shareType = 4;
            this.noDataShow = false;
            let params = {
                storeId: this.$route.query && this.$route.query.storeId ? this.$route.query.storeId : 0,
                shareId: this.$route.query && this.$route.query.shareId ? this.$route.query.shareId : 0
            }
            let shareId = 4; //----对应分享类型需要传的ID： 1： 媒体ID 2： 活动id 3:商品id 4:买家秀ID 5: 大师ID
            this.buyShowApi(Object.assign({}, params, { successFn }));
            function successFn () {
                setTimeout(() => {
                    if (that.buyShow) {
                        that.commontList = that.buyShow.commont;
                        that.pageMsg = that.buyShow.message;
                        setTimeout(() => {
                            that.noDataShow = true;
                        }, 500)
                    }
                }, 20)
            }
        },
        initBigMaster () {
            /**/
            if (!(this.menuType && this.menuId)) {
                return
            }
            let obj = {
                storeId: this.storeId,
                menuType: this.menuType,
                menuId: this.menuId,
            };
            let that = this;
            this.shareType = 5;
            this.noDataShow = false;
            let shareId = 5; //----对应分享类型需要传的ID： 1： 媒体ID 2： 活动id 3:商品id 4:买家秀ID 5: 大师ID
            let classId = obj && (obj.classId || obj.classId == 0) ? obj.classId : -1;
            let pageNum = obj && obj.pageNum ? obj.pageNum : 1;
            let tempObj = {
                storeId: this.storeId,
                classId: classId,
                pageNum: 1,
                menuId: obj.menuId,
                userId: this.userId,
                shareId: this.$route.query.shareId,
                successFn: this.masterSuccessFn,
            };
            this.masterListApi(tempObj);
        },
        masterSuccessFn () {
            let that = this;
            if (that.master) {
                let tempRtCommont = [];
                that.master.rtCommont.forEach((value) => {
                    tempRtCommont.push(Object.assign({}, value, { likeType: that.$route.query.menuType }))
                })
                // = Object.assign({},that.master.rtCommont,);
                that.commontList = tempRtCommont;
                // that.pageMsg = that.master.masterMsg;
                that.pageMsg = Object.assign({}, that.pageMsg, that.master.masterMsg);
                setTimeout(() => {
                    that.noDataShow = true;
                }, 500)
            }
        },
        showMarsterDetail () {
            let that = this;
            this.bottomSuspensionShow = true;
            this.swiperIndex = this.$refs.fullImgVideoSwiper.swiperIndex;
            setTimeout(() => {
                that.$refs.bottomSuspension[0].showMarsterDetail()
            }, 50)
        },
        showMarsterClassify (obj) {
            let that = this;
            this.bottomSuspensionShow = true;
            this.swiperIndex = this.$refs.fullImgVideoSwiper.swiperIndex;
            setTimeout(() => {
                if (that.$refs.bottomSuspension && that.$refs.bottomSuspension[0] && that.$refs.bottomSuspension[0].showMarsterClassify) {
                    that.$refs.bottomSuspension[0].showMarsterClassify(obj)
                }
            }, 50)
        },
        toSwiperIndex (key) {
            if (this.$refs.fullImgVideoSwiper && this.$refs.fullImgVideoSwiper) {
                this.$refs.fullImgVideoSwiper.toSwiperIndex(key);
            }
        },
        wxShareCb (params) {
            let that = this;
            this.shareShow(false);
            if (!params) {
                console.log(params, 'wxParams');
                return
            }
            if (Array.isArray(that.commontList) && that.commontList.length > 0) {
                that.commontList.forEach((value, index) => {
                    if (params.shareId == value.id) {
                        value.dataInfo.shareNum += 1;
                        that.commontList.splice(index, 1, value)
                    }
                })
            }
        },
        openRedPage (obj, callback) {
            let that = this;
            if (!this.userId) {
                this.$router.push('/to/login');
                return
            }
            let url = "";
            /*普通红包*/
            if (obj && obj.welPacketType == 0) {
                url = `${this.NW_API_API_LOCALE_VERSION}/welfare/direct/packet/receive?userId=${this.userId}&giveChannel=${this.giveChannel}&welPacketId=${obj.welPacketId}`;
                /*助力红包*/
            } else if (obj && obj.welPacketType == 1) {
                url = `${this.NW_API_API_LOCALE_VERSION}/welfare/help/packet/receive?userId=${this.userId}&welPacketId=${obj.welPacketId}`
                /*成长红包*/
            } else if (obj && obj.welPacketType == 2) {
                url = `${this.NW_API_API_LOCALE_VERSION}/welfare/grow/packet/receive?userId=${this.userId}&welPacketId=${obj.welPacketId}`
            }
            this.xurl.put(url).then((data) => {
                if (data.success) {
                    for (let value in that.couponsList) {
                        if (that.couponsList[value].couponsProps.myPacketInfo && obj.welPacketId == that.couponsList[value].couponsProps.myPacketInfo.welPacketId) {
                            that.couponsList[value].couponsProps.myPacketInfo.canReceive = false;
                            that.couponsList[value].couponsProps.myPacketInfo.showReceivedAmount = true;
                            that.couponsList[value].couponsProps.myPacketInfo.receivedAmount = data.result;
                            that.couponsList[value].couponsProps.getBtnActive = false;
                            if (callback) {
                                callback();
                            }
                            that.couponsList.splice(value, 1, that.couponsList[value]);
                        }
                    }
                } else if (data.errMsg) {
                    this.$root.$emit("toast", { msg: data.errMsg })
                } else {
                    this.$root.$emit("toast", { msg: '领取红包失败' })
                }
            })
        },
        getTemplate (index, obj, cb) {
            //data-couponsId
            let that = this;
            if (!this.userId) {
                this.$router.push({ path: '/to/login', query: { 'isToPhoneLogin': true } });
            } else {
                if (obj.state === 0) {
                    this.$store.dispatch("getTemplateApi", obj.templateId).then((data) => {
                        if (data.success) {
                            if (cb) {
                                cb();
                            }
                            //这里是否需要刷新
                            that.couponsList.forEach((value, index) => {
                                if (Array.isArray(value.couponsProps)) {
                                    value.couponsProps.forEach((couponsItem, couponsIndex) => {
                                        if (couponsItem.templateId == obj.templateId) {
                                            that.couponsList[index].couponsProps[couponsIndex].state = 7;
                                            that.couponsList = that.couponsList.splice(index, 1, that.couponsList[index]);
                                            let couponsList = that.couponsList;
                                            setTimeout(() => {
                                                that.couponsList = [];
                                                setTimeout(() => {
                                                    that.couponsList = couponsList;
                                                }, 10)
                                            }, 1000)
                                        }
                                    })
                                }
                            })
                            this.$root.$emit('toast', { msg: '领取成功' })
                        } else if (data.errMsg) {
                            this.$root.$emit('toast', { msg: data.errMsg })
                        } else {
                            this.$root.$emit('toast', { msg: '领取失败' })
                        }
                    })
                } else if (obj.state === 6) {
                    //优惠券详情页面/coupon/template/detail/
                    // this.$router.push({path:'/coupon/template/detail/'+obj.templateId+'/none'})
                } else {
                    this.$root.$emit('toast', { msg: "此券不可领取" });
                }
            }
        },
        suspensionCb (index) {
            let tempObj = this.couponsList[index];
            let tempObj1 = {
                isShow: false
            }
            tempObj = Object.assign({}, tempObj, tempObj1);
            this.couponsList.splice(index, 1, tempObj)
        },
        animationCb (index) {
            let tempObj = this.couponsList[index];
            let tempObj1 = {
                isShow: true
            };
            tempObj = Object.assign({}, tempObj, tempObj1);
            this.couponsList.splice(index, 1, tempObj)
        },
        getAllCounponsFn (obj, cb) {
            if (!this.userId) {
                this.$router.push({ path: '/to/login', query: { 'isToPhoneLogin': true } });
                return
            }
            let tempI = 0;
            let that = this;
            let templatIds = [];
            for (let value of obj) {
                if (value.state == 0) {
                    templatIds.push(value.templateId);
                }
            }
            let params = {
                ids: templatIds
            };
            if (templatIds.length == 0) {
                this.$root.$emit('toast', { msg: `领取失败或不可领取` });
                return;
            }
            let i = 0;
            let time = '';
            if (cb) {
                cb();
            }
            this.$store.dispatch('getAllCounponsFnApi', params).then((data) => {
                if (data.success) {
                    if (data.result.failNum && data.result.successNum) {
                        time = setInterval(() => {
                            // that.templateHide(i);
                            for (let value in that.$refs.coupons) {
                                for (let objValue in obj) {
                                    if (obj[objValue].promotionId == that.$refs.coupons[value].templates[0].promotionId) {
                                        that.$refs.coupons[value].templateHide(objValue, obj[objValue]);
                                    }
                                }
                            }
                            that.couponsList.forEach((value, index) => {
                                if (Array.isArray(value.couponsProps)) {
                                    value.couponsProps.forEach((couponsItem, couponsIndex) => {
                                        obj.forEach((AryValue, AryIndex) => {
                                            if (couponsItem.templateId == AryValue.templateId && that.couponsList[index] && that.couponsList[index].couponsProps) {
                                                that.couponsList[index].couponsProps[couponsIndex].state = 7;
                                                if (tempI == 0) {
                                                    tempI = 1
                                                    let couponsList = that.couponsList;
                                                    setTimeout(() => {
                                                        setTimeout(() => {
                                                            that.couponsList = couponsList;
                                                        }, 10)
                                                    }, 1000)
                                                }
                                            }
                                        })
                                    })
                                }
                            })
                            i++;
                            if (i >= obj.length) {
                                clearInterval(time)
                                i = 0
                            }
                        }, 400);
                        this.$root.$emit('toast', { msg: `成功领取${data.result.successNum}张券，${data.result.failNum}未领取` });
                        setTimeout(() => {
                            this.getPageMsg();
                        }, 1500)
                    } else if (!data.result.failNum && data.result.successNum) {
                        time = setInterval(() => {
                            /*定位是那个页面的劵*/
                            for (let value in that.$refs.coupons) {
                                for (let objValue in obj) {
                                    if (obj[objValue].promotionId == that.$refs.coupons[value].templates[0].promotionId) {
                                        that.$refs.coupons[value].templateHide(objValue, obj[objValue]);
                                    }
                                }
                            }
                            that.couponsList.forEach((value, index) => {
                                if (Array.isArray(value.couponsProps)) {
                                    value.couponsProps.forEach((couponsItem, couponsIndex) => {
                                        obj.forEach((AryValue, AryIndex) => {
                                            if (couponsItem.templateId == AryValue.templateId && that.couponsList[index] && that.couponsList[index].couponsProps) {
                                                that.couponsList[index].couponsProps[couponsIndex].state = 7;
                                                if (tempI == 0) {
                                                    tempI = 1
                                                    let couponsList = that.couponsList;
                                                    setTimeout(() => {
                                                        setTimeout(() => {
                                                            that.couponsList = couponsList;
                                                        }, 10)
                                                    }, 1000)
                                                }
                                            }
                                        })
                                    })
                                }
                            })
                            i++;
                            if (i >= obj.length) {
                                clearInterval(time)
                                i = 0
                            }
                        }, 400);
                        this.$root.$emit('toast', { msg: `成功领取${data.result.successNum}张券` });
                    } else if (!data.result.successNum && data.result.failNum) {
                        this.$root.$emit('toast', { msg: `领取失败或不可领取` });
                    }
                } else if (data.errMsg) {
                    this.$root.$emit('toast', { msg: data.errMsg });
                } else {
                    this.$root.$emit('toast', "领取失败");
                }
            })
        },
        getPageMsg (obj) {
            this.$emit('getPageMsg', obj);
        },
        // isLike(obj){
        //     this.isLike(obj)
        //     // this.$emit('isLike',obj);
        // },
        showShare (obj) {
            this.$emit('showShare', obj);
        },
        doAppraise (appraiseValue, obj) {
            /*先判断是否登录，然后再判断是否输入了估价的金额*/
            if (!this.userId) {
                this.$router.push({ path: '/to/login' });
                return
            }
            if (appraiseValue != 0 && (!appraiseValue)) {
                this.$root.$emit('toast', { msg: '请输入平均价' });
                return
            }
            /*不为数字*/
            let regVal = /^[0-9\.]+$/ig;
            if (!regVal.test(appraiseValue)) {
                this.$root.$emit('toast', { msg: '价格只能为数字' });
                return
            }
            let params = {
                fillerValue: appraiseValue,
                actKey: obj.actKey
            }
            this.$store.dispatch('doAppraiseApi', params).then((data) => {
                console.log(data, '99999');

                if (data.success) {
                    // this.interact
                    // let tempObj = {
                    //     show:true,
                    //     averageValue:data.result.averageValue,
                    //     appraiseValue:appraiseValue,
                    //     validNum:data.result.validNum,
                    //     actLimitJoin:data.result.actLimitJoin,
                    // }
                    // this.$refs.fullImgVideoSwiper.updateAppraiseInfo(tempObj);
                    this.$router.push({ path: '/act/finish' })
                } else if (data.errMsg) {
                    this.$root.$emit('toast', { msg: data.errMsg });
                } else {
                    this.$root.$emit('toast', { msg: '估价失败' });
                }
            })
        },
    },
    watch: {
        /*这里为什么要调用大师的内容*/
        // storeMenu(val){
        //     if(val.menuId || val.menuType){
        //         this.initBigMaster();
        //     }
        // }
        master (val) {
            if (val && val.masterMsg && Array.isArray(val.masterMsg.list) && val.masterMsg.list.length > 0) {
                this.showSwiper = false
                setTimeout(() => {
                    this.masterSuccessFn();
                    this.showSwiper = true;
                }, 10)
            }
        }
    },
    components: {
        'full-img-video-swiper': FullImgVideoSwiper,
        'video-float-footer': () => import("../../components/commonActModel/panoramaVideoSwiper/VideoFloatFooter"),
        "v-no-data": () => import("../../components/commonActModel/noData/NoData"),
        'bottom-suspension': () => import("./components/master/Bottom"),
        'wx-share-body': () => import('../../components/wx/WxShareBody'),
    }
}
</script>

<style scoped lang="scss">
.store-activity /deep/ {
    .no-wcomment {
        z-index: 1;
    }
    .look-index-mood {
        position: relative !important;
        padding: 0 !important;
    }
}
.no-data-wrap {
    height: 100vh;
}
</style>